package com.example.myutil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class TreeListUtil {

	public static Map<String, Object> createTreeMapAddRoot(
			List<Map<String, String>> dataList, String root_id) {
		Map<String, Object> retMap = new HashMap<String, Object>();
		Map<String, Object> rootMap = new LinkedHashMap<String, Object>();
		Map<String, Object> childMap = new HashMap<String, Object>();
		
		for(Map<String, String> temp : dataList){
			if(root_id.equals(temp.get("id"))){
				rootMap.put("id", temp.get("id"));
				rootMap.put("name", temp.get("text"));
				rootMap.put("level", "0");
			}
		}
		
		Map<String, Object> childList = createTreeMap(dataList, root_id);
		if(childList.size()>0){
			rootMap.put("type", "folder");
			childMap.put("children", childList);
		}else{
			rootMap.put("type", "item");
		}
		rootMap.put("additionalParameters", childMap);
		retMap.put(rootMap.get("name").toString(), rootMap);
		return retMap;
	}

	public static Map<String, Object> createTreeMapAddRootWithNone(
			List<Map<String, String>> dataList, String root_id) {
		Map<String, Object> retMap = new HashMap<String, Object>();
		Map<String, Object> rootMap = new LinkedHashMap<String, Object>();
		Map<String, Object> childMap = new HashMap<String, Object>();
		
		rootMap.put("id", root_id);
		rootMap.put("name", "根节点");
		rootMap.put("level", "0");
	
		Map<String, Object> childList = createTreeMap(dataList, root_id);
		if(childList.size()>0){
			rootMap.put("type", "folder");
			childMap.put("children", childList);
		}else{
			rootMap.put("type", "item");
		}
		rootMap.put("additionalParameters", childMap);
		retMap.put(rootMap.get("name").toString(), rootMap);
		return retMap;
	}

	public static Map<String, Object> createTreeMap(
			List<Map<String, String>> dataList, String fid) {
		Map<String, Object> treeList = new LinkedHashMap<String, Object>();
		for (int i = 0; i < dataList.size(); i++) {
			Map<String, Object> map = null;
			Map<String, Object> child = null;
			
			Map<String, String> node = dataList.get(i);
			if ((node.get("pid")).equals(fid)) {
				map = new HashMap<String, Object>();
				child = new HashMap<String, Object>();
				
				map.put("name", node.get("text"));
				Map<String, Object> childs = createChildrenTreeMap(dataList, node.get("id"));
				if(childs.size()>0){
					map.put("type", "folder");
					child.put("children", childs);
				}else{
					map.put("type", "item");
				}
				
				/** 标签树需要用到的几个属性 **/
				if(null!=node.get("level") && !"".equals(node.get("level"))){
					map.put("level", node.get("level"));
				}
				if(null!=node.get("def") && !"".equals(node.get("def"))){
					map.put("def", node.get("def"));
				}
				if(null!=node.get("alias") && !"".equals(node.get("alias"))){
					map.put("alias", node.get("alias"));
				}
				/** end 标签树 **/
				
				map.put("id", node.get("id"));
				map.put("additionalParameters", child);
			}
			if (map != null) {
				treeList.put(map.get("name").toString(), map);
			}
		}
		return treeList;
	}

	private static Map<String, Object> createChildrenTreeMap(
			List<Map<String, String>> dataList, String fid) {
		Map<String, Object> childList = new LinkedHashMap<String, Object>();
		for (int j = 0; j < dataList.size(); j++) {
			Map<String, Object> map = null;
			Map<String, Object> child = null;
			
			Map<String, String> treeChild = dataList.get(j);
			if ((treeChild.get("pid")).equals(fid)) {
				map = new HashMap<String, Object>();
				child = new HashMap<String, Object>();
				
				map.put("name", treeChild.get("text"));
				Map<String, Object> childs = createChildrenTreeMap(dataList, treeChild.get("id"));
				if(childs.size()>0){
					map.put("type", "folder");
					child.put("children", childs);
				}else{
					map.put("type", "item");
				}

				/** 标签树需要用到的几个属性 **/
				if(null!=treeChild.get("level") && !"".equals(treeChild.get("level"))){
					map.put("level", treeChild.get("level"));
				}
				if(null!=treeChild.get("def") && !"".equals(treeChild.get("def"))){
					map.put("def", treeChild.get("def"));
				}
				if(null!=treeChild.get("alias") && !"".equals(treeChild.get("alias"))){
					map.put("alias", treeChild.get("alias"));
				}
				/** end 标签树 **/
				
				map.put("id", treeChild.get("id"));
				map.put("additionalParameters", child);
			}

			if (map != null){
				childList.put(map.get("name").toString(), map);
			}
		}
		return childList;
	}

	public static List<Map<String, Object>> createTreeListMap(
			List<Map<String, String>> dataList, String fid) {
		List<Map<String, Object>> treeList = new ArrayList<Map<String, Object>>();
		for (int i = 0; i < dataList.size(); i++) {
			Map<String, Object> map = null;
			
			Map<String, String> node = dataList.get(i);
			if ((node.get("pid")).equals(fid)) {
				map = new HashMap<String, Object>();
				
				map.put("name", node.get("text"));
				List<Map<String, Object>> childsList = createChildrenTreeListMap(dataList, node.get("id"));
				if(childsList.size()>0){
					map.put("type", "folder");
				}else{
					map.put("type", "item");
				}
				map.put("id", node.get("id"));
				map.put("childs", childsList);
				
				/** 标签树需要用到的几个属性 **/
				if(null!=node.get("level") && !"".equals(node.get("level"))){
					map.put("level", node.get("level"));
				}
				if(null!=node.get("def") && !"".equals(node.get("def"))){
					map.put("def", node.get("def"));
				}
				if(null!=node.get("alias") && !"".equals(node.get("alias"))){
					map.put("alias", node.get("alias"));
				}
				/** end 标签树 **/
			}
			if (map != null) {
				treeList.add(map);
			}
		}
		return treeList;
	}

	private static List<Map<String, Object>> createChildrenTreeListMap(
			List<Map<String, String>> dataList, String fid) {
		List<Map<String, Object>> treeList = new ArrayList<Map<String, Object>>();
		for (int j = 0; j < dataList.size(); j++) {
			Map<String, Object> map = null;
			Map<String, String> treeChild = dataList.get(j);
			if ((treeChild.get("pid")).equals(fid)) {
				map = new HashMap<String, Object>();
				
				map.put("name", treeChild.get("text"));
				List<Map<String, Object>> childsList = createChildrenTreeListMap(dataList, treeChild.get("id"));
				if(childsList.size()>0){
					map.put("type", "folder");
				}else{
					map.put("type", "item");
				}
				map.put("id", treeChild.get("id"));
				map.put("childs", childsList);

				
				/** 标签树需要用到的几个属性 **/
				if(null!=treeChild.get("level") && !"".equals(treeChild.get("level"))){
					map.put("level", treeChild.get("level"));
				}
				if(null!=treeChild.get("def") && !"".equals(treeChild.get("def"))){
					map.put("def", treeChild.get("def"));
				}
				if(null!=treeChild.get("alias") && !"".equals(treeChild.get("alias"))){
					map.put("alias", treeChild.get("alias"));
				}
				/** end 标签树 **/
			}

			if (map != null){
				treeList.add(map);
			}
		}
		return treeList;
	}

}
